|
1
|
|
|
import * as sinon from 'sinon'; |
|
2
|
|
|
import L from 'leaflet'; |
|
3
|
|
|
import marker2x from 'leaflet/dist/images/marker-icon-2x.png'; |
|
4
|
|
|
import marker from 'leaflet/dist/images/marker-icon.png'; |
|
5
|
|
|
import markerShadow from 'leaflet/dist/images/marker-shadow.png'; |
|
6
|
|
|
import { |
|
7
|
|
|
stateFlagTimeout as stateFlagTimeoutFactory, |
|
|
|
|
|
|
8
|
|
|
determineOrderDir, |
|
9
|
|
|
fixLeafletIcons, |
|
10
|
|
|
rangeOf, |
|
11
|
|
|
roundTen, |
|
12
|
|
|
} from '../../src/utils/utils'; |
|
13
|
|
|
|
|
14
|
|
|
describe('utils', () => { |
|
15
|
|
|
describe('stateFlagTimeout', () => { |
|
16
|
|
|
it('sets state and initializes timeout with provided delay', () => { |
|
17
|
|
|
const setTimeout = sinon.fake((callback) => callback()); |
|
18
|
|
|
const setState = sinon.spy(); |
|
19
|
|
|
const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout); |
|
20
|
|
|
const delay = 5000; |
|
21
|
|
|
const expectedSetStateCalls = 2; |
|
22
|
|
|
|
|
23
|
|
|
stateFlagTimeout(setState, 'foo', false, delay); |
|
24
|
|
|
|
|
25
|
|
|
expect(setState.callCount).toEqual(expectedSetStateCalls); |
|
26
|
|
|
expect(setState.getCall(0).args).toEqual([{ foo: false }]); |
|
27
|
|
|
expect(setState.getCall(1).args).toEqual([{ foo: true }]); |
|
28
|
|
|
expect(setTimeout.callCount).toEqual(1); |
|
29
|
|
|
expect(setTimeout.getCall(0).args[1]).toEqual(delay); |
|
30
|
|
|
}); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
describe('determineOrderDir', () => { |
|
34
|
|
|
it('returns ASC when current order field and selected field are different', () => { |
|
35
|
|
|
expect(determineOrderDir('foo', 'bar')).toEqual('ASC'); |
|
36
|
|
|
expect(determineOrderDir('bar', 'foo')).toEqual('ASC'); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
it('returns ASC when no current order dir is provided', () => { |
|
40
|
|
|
expect(determineOrderDir('foo', 'foo')).toEqual('ASC'); |
|
41
|
|
|
expect(determineOrderDir('bar', 'bar')).toEqual('ASC'); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
it('returns DESC when current order field and selected field are equal and current order dir is ASC', () => { |
|
45
|
|
|
expect(determineOrderDir('foo', 'foo', 'ASC')).toEqual('DESC'); |
|
46
|
|
|
expect(determineOrderDir('bar', 'bar', 'ASC')).toEqual('DESC'); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
it('returns undefined when current order field and selected field are equal and current order dir is DESC', () => { |
|
50
|
|
|
expect(determineOrderDir('foo', 'foo', 'DESC')).toBeUndefined(); |
|
51
|
|
|
expect(determineOrderDir('bar', 'bar', 'DESC')).toBeUndefined(); |
|
52
|
|
|
}); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
describe('fixLeafletIcons', () => { |
|
56
|
|
|
it('updates icons used by leaflet', () => { |
|
57
|
|
|
fixLeafletIcons(); |
|
58
|
|
|
|
|
59
|
|
|
const { iconRetinaUrl, iconUrl, shadowUrl } = L.Icon.Default.prototype.options; |
|
60
|
|
|
|
|
61
|
|
|
expect(iconRetinaUrl).toEqual(marker2x); |
|
62
|
|
|
expect(iconUrl).toEqual(marker); |
|
63
|
|
|
expect(shadowUrl).toEqual(markerShadow); |
|
64
|
|
|
}); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
describe('rangeOf', () => { |
|
68
|
|
|
const func = (i) => `result_${i}`; |
|
69
|
|
|
const size = 5; |
|
70
|
|
|
|
|
71
|
|
|
it('builds a range of specified size invike provided function', () => { |
|
72
|
|
|
expect(rangeOf(size, func)).toEqual([ |
|
73
|
|
|
'result_1', |
|
74
|
|
|
'result_2', |
|
75
|
|
|
'result_3', |
|
76
|
|
|
'result_4', |
|
77
|
|
|
'result_5', |
|
78
|
|
|
]); |
|
79
|
|
|
}); |
|
80
|
|
|
|
|
81
|
|
|
it('builds a range starting at provided pos', () => { |
|
82
|
|
|
const startAt = 3; |
|
83
|
|
|
|
|
84
|
|
|
expect(rangeOf(size, func, startAt)).toEqual([ |
|
85
|
|
|
'result_3', |
|
86
|
|
|
'result_4', |
|
87
|
|
|
'result_5', |
|
88
|
|
|
]); |
|
89
|
|
|
}); |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
describe('roundTen', () => { |
|
93
|
|
|
it('rounds provided number to the next multiple of ten', () => { |
|
94
|
|
|
const expectationsPairs = [ |
|
95
|
|
|
[ 10, 10 ], |
|
96
|
|
|
[ 12, 20 ], |
|
97
|
|
|
[ 158, 160 ], |
|
98
|
|
|
[ 5, 10 ], |
|
99
|
|
|
[ -42, -40 ], |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
|
|
expect.assertions(expectationsPairs.length); |
|
|
|
|
|
|
103
|
|
|
expectationsPairs.forEach(([ number, expected ]) => { |
|
104
|
|
|
expect(roundTen(number)).toEqual(expected); |
|
105
|
|
|
}); |
|
106
|
|
|
}); |
|
107
|
|
|
}); |
|
108
|
|
|
}); |
|
109
|
|
|
|